package com.hkbigdata.transform;

import com.hkbigdata.bean.WaterSensor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

/**
 * @author liuanbo
 * @creat 2024-04-11-15:51
 * @see 2194550857@qq.com
 */
public class Flink08_TransForm_Aggregate_Anonymous {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

/*        DataStreamSource<Integer> integerDataStreamSource = env.fromElements(1, 2, 3, 4, 5, 6);
        KeyedStream<Integer, String> integerStringKeyedStream = integerDataStreamSource.keyBy(data -> data % 2 == 0 ? "偶数" : "奇数");
        integerStringKeyedStream.sum(0).print("sum");
        integerStringKeyedStream.max(0).print("max");
        integerStringKeyedStream.min(0).print("min");*/

        List<WaterSensor> waterSensors = Arrays.asList(
                new WaterSensor("ws_001", 1607527992000L, 20),
                new WaterSensor("ws_001", 1607527992001L, 22),
                new WaterSensor("ws_001", 1607527992002L, 33),
                new WaterSensor("ws_002", 1607527992003L, 44),
                new WaterSensor("ws_002", 1607527992004L, 22),
                new WaterSensor("ws_002", 1607527992001L, 50)
        );

        KeyedStream<WaterSensor, String> waterSensorStringKeyedStream = env.fromCollection(waterSensors)
                .keyBy(new KeySelector<WaterSensor, String>() {
                    @Override
                    public String getKey(WaterSensor value) throws Exception {
                        return value.getId();
                    }
                });

        waterSensorStringKeyedStream.sum("vc").print();
        waterSensorStringKeyedStream.maxBy("vc", false).print();
        waterSensorStringKeyedStream.minBy("vc", true).print();


        env.execute();


    }
}
